package la.iok.hzsvn.lewin.movie.controller;

import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.entity.BookChapter;
import la.iok.hzsvn.lewin.movie.entity.MovieInstance;
import la.iok.hzsvn.lewin.movie.entity.PlayRoleBook;
import la.iok.hzsvn.lewin.movie.entity.PlayRoleBookImage;
import la.iok.hzsvn.lewin.movie.model.FileVo;
import la.iok.hzsvn.lewin.movie.service.*;
import la.iok.hzsvn.share.response.ListDataResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("playRoleBookImages")
public class PlayRoleBookImageController {
    private final MovieDownloadService movieDownloadService;
    private final PlayRoleBookService playRoleBookService;
    private final BookChapterService bookChapterService;
    private final PlayRoleBookImageService playRoleBookImageService;
    private final PlayRoleBookInstanceService playRoleBookInstanceService;
    private final MovieInstanceService movieInstanceService;

    public PlayRoleBookImageController(MovieDownloadService movieDownloadService,
                                       PlayRoleBookService playRoleBookService,
                                       BookChapterService bookChapterService,
                                       PlayRoleBookImageService playRoleBookImageService,
                                       PlayRoleBookInstanceService playRoleBookInstanceService,
                                       MovieInstanceService movieInstanceService) {
        this.movieDownloadService = movieDownloadService;
        this.playRoleBookService = playRoleBookService;
        this.bookChapterService = bookChapterService;
        this.playRoleBookImageService = playRoleBookImageService;
        this.playRoleBookInstanceService = playRoleBookInstanceService;
        this.movieInstanceService = movieInstanceService;
    }

    @GetMapping("playRoleBook/{id}")
    public ListDataResponse<FileVo> list(@RequestHeader Token token, @PathVariable Long id){
        PlayRoleBook book = playRoleBookService.select(id);
        if(book == null){
            return new ListDataResponse<>(Collections.emptyList());
        }
        BookChapter bookChapter = bookChapterService.select(book.getChapterId());
        if(bookChapter == null){
            return new ListDataResponse<>(Collections.emptyList());
        }
        Long movieInstanceId = movieDownloadService.loadDownloadInstance(token,bookChapter.getMovieId());
        List<PlayRoleBookImage> images = playRoleBookInstanceService.images(movieInstanceId,id);
        return new ListDataResponse<>(images.stream().map(po->{
            FileVo vo = new FileVo();
            BeanUtils.copyProperties(po,vo);
            return vo;
        }).collect(Collectors.toList()));
    }

    @GetMapping("movie/{movieId}/v/{version}/playRoleBook/{id}")
    public ListDataResponse<FileVo> list2(@PathVariable Long movieId, @PathVariable String version, @PathVariable Long id){
        MovieInstance mi = movieInstanceService.movieInst2(movieId,version);
        List<PlayRoleBookImage> images = playRoleBookInstanceService.images(mi.getId(),id);
        return new ListDataResponse<>(images.stream().map(po->{
            FileVo vo = new FileVo();
            BeanUtils.copyProperties(po,vo);
            return vo;
        }).collect(Collectors.toList()));
    }

    /**
     * 查询图片文件摘要信息
     * @param id 角色id
     * @return 角色文件摘要信息
     */
    @GetMapping("{id}/digest")
    public SingleDataResponse<String> digest(@PathVariable Long id){
        return OperatorResponses.stringSingleDataResponse(playRoleBookImageService.digest(id));
    }

    /**
     * 查询图片文件存储路径
     * @param id 角色id
     * @return 角色文件存储路径
     */
    @GetMapping("{id}/storePath")
    public SingleDataResponse<String> storePath(@PathVariable Long id){
        return OperatorResponses.stringSingleDataResponse(playRoleBookImageService.storePath(id));
    }

    /**
     * 下载图片文件
     * @param response 响应
     * @param id 角色id
     */
    @GetMapping("{id}/file")
    public void downloadFile(HttpServletResponse response, @PathVariable Long id){
        playRoleBookImageService.downloadFile(response,id);
    }
}
